package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setMealService.saveSetmealAndDish(setmealDto);
        return R.success("添加成功");
    }
    @GetMapping("/page")//分页查询
    public R<Page> page(int page,int pageSize,String name){
        log.info("分页查询");
        //创建分页构造器
        Page<Setmeal> infoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getCreateTime);
        setMealService.page(infoPage,wrapper);
        Page<SetmealDto>  page1= new Page<>();
        BeanUtils.copyProperties(infoPage,page1,"records");
        //当前页数据
        List<SetmealDto> records = new ArrayList<>();
        List<Setmeal> records1 = infoPage.getRecords();
        for (final Setmeal setmeal : records1) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category byId = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(byId.getName());
            records.add(setmealDto);
        }
        page1.setRecords(records);
        return R.success(page1);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除");
        setMealService.removes(ids);
        return R.success("删除成功");
    }
    @GetMapping("/{id}") //修改操作 url参数是restful风格
    public R<SetmealDto> getById(@PathVariable Long id){
        log.info("套餐回显");
        SetmealDto setmealAndSetmealDish = setMealService.getSetmealAndSetmealDish(id);
        return R.success(setmealAndSetmealDish);
    }
    @PutMapping //修改操作 url参数是restful风格
    public R<String> getById(@RequestBody SetmealDto setmealDto){
        log.info("套餐修改");
        setMealService.PutSetmealAndSetmealDish(setmealDto);
        return R.success("修改成功");
    }
    @GetMapping("/list")//修改操作 url参数是restful风格
    public R<List<Setmeal>> getList(Setmeal setmeal){
        log.info("套餐查询");
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setMealService.list(wrapper);
        return R.success(list);
    }
}
